#!/usr/bin/env python
#-*- coding:utf-8 -*-

# file:insecure_courseController.py
# author:86133
# datetime:2022/6/9 9:45
# software: PyCharm
'''
Insecure CourseController
'''
# !/usr/bin/env python
# -*- coding:utf-8 -*-

import datetime
import math
import json
import pymysql

from sqlalchemy import or_

from app import db
from app.api_1_0 import limiter
from ..models.courseModel import Course
from ..utils import commons
from ..utils.response_code import RET, error_map_EN
from ..utils.loggings import loggings


class InsecureCourseController(Course):

    # get

    @classmethod
    @limiter.exempt
    def get(cls, **kwargs):
        try:
            filter_list = [cls.isDelete == 0]
            if kwargs.get('courseID'):
                filter_list.append(cls.courseID == kwargs['courseID'])
            else:
                if kwargs.get('courseName'):
                    filter_list.append(cls.courseName == kwargs.get('courseName'))
                if kwargs.get('teacherID'):
                    filter_list.append(cls.teacherID == kwargs.get('teacherID'))
                if kwargs.get('count') is not None:
                    filter_list.append(cls.count == kwargs.get('count'))
                if kwargs.get('credit') is not None:
                    filter_list.append(cls.credit == kwargs.get('credit'))
                if kwargs.get('addTime'):
                    filter_list.append(cls.addTime == kwargs.get('addTime'))

            page = int(kwargs.get('Page', 1))
            size = int(kwargs.get('Size', 10))

            course_info = db.session.query(cls).filter(*filter_list)

            count = course_info.count()
            pages = math.ceil(count / size)
            course_info = course_info.limit(size).offset((page - 1) * size).all()

            results = commons.query_to_dict(course_info)
            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'totalCount': count, 'totalPage': pages,
                    'data': results}

        except Exception as e:
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'data': {'error': str(e)}}
        finally:
            db.session.close()


    # sql get
    @classmethod
    def sql_get(cls, **kwargs):
        db = pymysql.connect(host='127.0.0.1',
                             user='root',
                             password='730523.cly',
                             database='python_project')
        cur = db.cursor()
        sql = 'SELECT * FROM course WHERE courseID=%s' % (kwargs['courseID'])
        print(sql)
        try:
            cur.execute(sql)
            # 获取所有记录列表
            course_info = cur.fetchall()
            # results = commons.query_to_dict(course_info)
            if course_info:
                return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'data': course_info}
        except Exception as e:
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'data': {'error': str(e)}}
        finally:
            db.close()
